package com.systop.dxjj.modules.user.service.impl;


import com.systop.dxjj.common.pojo.*;
import com.systop.dxjj.common.utils.JwtUtil;
import com.systop.dxjj.common.utils.R;
import com.systop.dxjj.common.utils.RedisCache;
import com.systop.dxjj.modules.user.mapper.UserMapper;
import com.systop.dxjj.modules.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.*;

/**
 * @author 吴健伟
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    @Qualifier("useauthenticationManagerBean")
    private AuthenticationManager useauthenticationManager;
    @Override
    public R selectAllUsers() {
        List<User> list = userMapper.selectAllUsers();
        return R.ok().put("users",list);
    }

    @Override
    public R loginUser(User user) {
        User u = userMapper.loginUser(user);
        if (u == null) {
            return R.ok().put("start","0");
        }else {
            return R.ok().put("start","1");
        }
    }

    @Override
    public R selectUserById(Integer id) {

        User user = userMapper.selectUserById(id);
        return R.ok().put("users",user);
    }

    @Override
    public R selectUserByName(String name) {
        User user = userMapper.selectUserByName(name);
        return R.ok().put("users",user);
    }

    //管理员注册
    @Override
    public R insertUser(User user) {
        User result = userMapper.selectUserByName(user.getUsername());
        //如果结果不为null，则抛出用户名被占用异常
        if (result != null) {
            return R.ok().put("start","0");
        }else {
            int rows = userMapper.insertUser(user);
            if (rows!=1){
                return R.ok().put("start","0");
            }else {
                return R.ok().put("start","1");
            }
        }
    }


    @Override
    public R updateUser(User user) {

        int num = userMapper.updateUser(user);
        if (num > 0){
            return R.ok().put("start","1");
        }else {
            return R.ok().put("start","0");
        }
    }

    @Override
    public ResponseResult userLogin(User user) {
        //进行用户认证
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(user.getUsername(),user.getPassword());
        useauthenticationManager.authenticate(usernamePasswordAuthenticationToken);


        Authentication authenticate = useauthenticationManager.authenticate(usernamePasswordAuthenticationToken);
        //如果认证没通过给出提示
        if (Objects.isNull(authenticate)){
            throw new RuntimeException("登录失败");
        }
        //如果认知通过，使用stuid生成jwt，jwt存入ResponseResult返回
        Useruse useruse = (Useruse)authenticate.getPrincipal();
        String userid = useruse.getUser().getUserid().toString();
        String jwt = JwtUtil.createJWT(userid);
        //把完整的用户信息存入redis，userid作为key
        Map<String, String> map = new HashMap<>();
        map.put("token",jwt);
        //把完整的用户信息存入redis，stuid作为key
        redisCache.setCacheObject("uselogin:"+userid,useruse);
        return new ResponseResult(200,"登陆成功！",map);
    }

    @Override
    public R deleteUser(Integer id) {

        int num = userMapper.deleteUser(id);
        if (num > 0){
            return R.ok().put("start","1");
        }else {
            return R.ok().put("start","0");
        }
    }

}
